package com.ruoyi;

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.net.ServerSocket;
import java.net.Socket;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: yangxuqi
 * @Date: 2022/11/17/9:16
 * @Description:
 */
@Component
public class RuoYiSocketInitializer implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments args) throws Exception {
        try {
            ServerSocket serverSocket = new ServerSocket(9100);//持续监听接口
            System.out.println("启动服务器....端口为:" + 9100 + " wait connect...");
            Socket socket = null;

            while (true) {
                socket = serverSocket.accept();//建立socket连接
                System.out.println("收到客户端连接，客户端:" + socket.getInetAddress().getHostAddress() + "已连接到服务器");
                ServerThread serverThread = new ServerThread(socket);
                serverThread.start();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
